package cn.tworice.client.web;

import cn.tworice.client.service.GradesService;
import cn.tworice.common.entity.Grades;
import cn.tworice.common.entity.Paper;
import cn.tworice.common.entity.dto.GradesDto;
import cn.tworice.common.entity.dto.PaperDto;
import com.alibaba.fastjson.JSONArray;
import com.lmk.core.web.support.api.RequestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("client/grades")
@CrossOrigin
public class GradesClient {
    @Autowired
    private GradesService gradesService;

    /**
     * 获取所有用户未批阅的答卷
     * 只能获取到已经结束的考卷信息
     *
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/26 15:47
     */
    @GetMapping("notPaper")
    public RequestResult paper(Integer page, Integer pageSize) {
        RequestResult result = new RequestResult();
        List<PaperDto> list = gradesService.getNotPaper(page, pageSize);
        result.appendData("list", list);
        result.appendData("total", gradesService.getNotPaperTotal());
        return result;
    }

    /**
     * 根据试卷ID和用户ID获取主观题答卷
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/26 18:25
     */
    @GetMapping("objectivePaper")
    public RequestResult objectivePaper(Paper paper){
        RequestResult result = new RequestResult();
        List<PaperDto> list=gradesService.getObjectivePaper(paper);
        result.appendData("list", list);
        return result;
    }

    @PutMapping("")
    public RequestResult set(String grades,String examId,String adminId){
        RequestResult result = new RequestResult();
        List<Grades> list = JSONArray.parseArray(grades, Grades.class);
        gradesService.setGrades(list,examId,adminId);
        return result;
    }

    /**
     * 通过用户ID获取成绩列表
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/27 15:43
     */
    @GetMapping("")
    public RequestResult list(String adminId,Integer page,Integer pageSize){
        RequestResult result = new RequestResult();
        List<GradesDto> list=gradesService.getGrades(adminId,page,pageSize);
        Integer total=gradesService.getGradesTotal(adminId);
        result.appendData("list", list);
        result.appendData("total",total);
        return result;
    }
}
